package com.ynkbny.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynkbny.domain.Router;
import com.ynkbny.domain.RouterLow;
import com.ynkbny.domain.RouterUp;
import com.ynkbny.domain.User;
import com.ynkbny.mapper.RouterMapper;
import com.ynkbny.service.RouterService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zelin.wang
 * @description 路由实现类
 * @date 8/15/2023
 */
@Service
public class RouterServiceImpl  extends ServiceImpl<RouterMapper, RouterLow> implements RouterService {

    @Resource
    RouterMapper routerMapper;

    public List getRouterByRoleId (String roleId){
        List<Object> result = new ArrayList<>();
        Map<String,String> data = new HashMap<>();
        data.put("roleId",roleId);
        data.put("type","0");
        List<RouterLow> upData = routerMapper.selectRouterByRoleId(data);//查询父级
        for (RouterLow item:upData){
            if(item.getComponent() != null){//判断是否只有一层
               result.add(item);
            }else{
                RouterUp up = new RouterUp();
                BeanUtils.copyProperties(item,up);
                data.put("roleId",roleId);
                data.put("type","1");
                data.put("upId",String.valueOf(item.getId()));
                up.setChildren(routerMapper.selectRouterByRoleId(data));
                result.add(up);
            }
        }
     return result;
    }

}
